from fastapi import FastAPI, File, UploadFile
from fastapi.staticfiles import StaticFiles
from typing import List
import uvicorn

app = FastAPI()


@app.post("/file1")
async def file1(file: bytes = File(...)):
    return f"文件长度: {len(file)}"


@app.post("/file2")
async def file1(file: UploadFile = File(...)):
    return f"文件名: {file.filename}, 文件大小: {len(await file.read())}"


@app.post("/file")
async def file(files: List[UploadFile] = File(...)):
    """指定类型为列表即可"""
    for idx, f in enumerate(files):
        files[idx] = f"文件名: {f.filename}, 文件大小: {len(await f.read())}"
    return files

# name 参数只是起一个名字，FastAPI 内部使用
app.mount("/static", StaticFiles(directory=r"../test/"), name="static")

if __name__ == "__main__":
    uvicorn.run("main3:app", host="0.0.0.0", port=5555)
